﻿using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.DependencyInjection;
using System.Threading.Tasks;

namespace Longbow.Web.SignalR
{
    /// <summary>
    /// 异常处理 SignalR 过滤器
    /// </summary>
    public class SignalRExceptionFilter<T> : IAsyncExceptionFilter
        where T : Hub
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task OnExceptionAsync(ExceptionContext context)
        {
            var handler = context.HttpContext.RequestServices.GetRequiredService<ISignalRExceptionFilterHandler<T>>();
            var hub = context.HttpContext.RequestServices.GetRequiredService<IHubContext<T>>();
            await handler.SendAsync(hub, context.Exception).ConfigureAwait(false);
        }
    }
}
